#import "SMRShadowImageCell.h"
#import "SMRBorderFrame.h"


@implementation SMRShadowImageCell

@synthesize borderFrame;


-(NSImageFrameStyle)imageFrameStyle {
	return NSImageFrameNone;
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
	NSImage *image = [self objectValue];
	if (!image)
		return;
	NSRect frameRect = cellFrame;
	frameRect.size = [self cellSizeForBounds:cellFrame];
	[[self borderFrame] drawFrameUnderImageInRect:frameRect];
	NSRect imageFrame = [self borderFrame] == nil ? frameRect : [[self borderFrame] imageRectForFrameRect:frameRect];
	[self drawInteriorWithFrame:imageFrame inView:controlView];
	[[self borderFrame] drawFrameOverImageInRect:frameRect];
}

-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
	[super drawInteriorWithFrame:cellFrame inView:controlView];
}


#pragma mark -
#pragma mark Layout

-(NSSize)cellSizeForBounds:(NSRect)aRect {
	NSImage *image = [self objectValue];
	if (!image)
		return aRect.size;
	NSSize imageSize = [image size];
	if ([self borderFrame]) {
		imageSize.width += 2 * [borderFrame horizontalPadding];
		imageSize.height += 2 * [borderFrame verticalPadding];
	}
	if (imageSize.width <= aRect.size.width && imageSize.height <= aRect.size.height)
		return aRect.size;
	CGFloat minHeight = fmin(aRect.size.height, imageSize.height);
	CGFloat cellWidth = floor(imageSize.width / imageSize.height * minHeight);
	NSSize cellSize = NSMakeSize(cellWidth, minHeight);
	return cellSize;
}

@end
